package service;

import java.util.List;

import dao.*;
import entity.equip;
import entity.hex;
import entity.hero;
import entity.trammel;

public class guide_service {
	
	private hero_dao d_hero=new hero_dao();
	private trammel_dao d_trammel=new trammel_dao();
	private hex_dao d_hex=new hex_dao();
	private equip_dao d_equip=new equip_dao();
	
	public List<hero> get_all_hero(String sql) {
		if (sql.equals("")){
			sql="select * from hero";
		}
		List<hero>res=d_hero.select(sql);
		if (res==null)return res;
		for (int i=0;i<res.size();i++){
			hero temp=res.get(i);
			temp=d_hero.find_hero_trammel(temp);
			String[] result = temp.getHero_attribute().split("\\$");
			String img_url=result[0];
			String attribute=result[1];
			temp.setImg_url(img_url);
			temp.setHero_attribute(attribute);
			res.set(i,temp);
		}
		return res;
	}
	public List<hero> get_hero_by_id(String hero_id){
		return get_all_hero("select * from hero where hero_id=\""+hero_id+"\"");
	}
	
	
	public List<trammel> get_all_trammel(String sql) {
		if (sql.equals("")){
			sql="select * from trammel";
		}
		List<trammel> res=d_trammel.select(sql);
		if (res==null)return res;
		for (int i=0;i<res.size();i++){
			trammel temp=res.get(i);
			temp=d_trammel.find_trammel_hero(temp);
			String[] result = temp.getTrammel_description().split("\\$");
			String img_url=result[0];
			String description=result[1];
			temp.setImg_url(img_url);
			temp.setTrammel_description(description);
			res.set(i,temp);
		}
		return res;
	}
	public List<trammel> get_trammel_by_id(String trammel_id){
		return get_all_trammel("select * from trammel where trammel_id=\""+trammel_id+"\"");
	}
	
	
	public List<hex> get_all_hex(String sql) {
		if (sql.equals("")){
			sql="select * from hex;";
		}
		List<hex> res=d_hex.select(sql);
		if (res==null)return res;
		for (int i=0;i<res.size();i++){
			hex temp=res.get(i);
			String[] result = temp.getHex_description().split("\\$|\\^");
			String type=result[0];
			String img_url=result[1];
			String description=result[2];
			temp.set_type(type);
			temp.setImg_url(img_url);
			temp.setHex_description(description);
			res.set(i,temp);
		}
		return res;
	}
	public List<hex> get_hex_by_id(String hex_id){
		return get_all_hex("select * from hex where hex_id=\""+hex_id+"\"");
	}
	
	
	public List<equip> get_all_equip(String sql) {
		if (sql.equals("")){
			sql="select * from equip";
		}
		List<equip> res=d_equip.select(sql);
		if (res==null)return res;
		for (int i=0;i<res.size();i++){
			equip temp=res.get(i);
			temp=d_equip.find_subequip(temp);
			String[] result = temp.getEquip_description().split("\\$|\\^");
			String type=result[0];
			String img_url=result[1];
			String description=result[2];
			temp.setType(type);
			temp.setImg_url(img_url);
			temp.setEquip_description(description);
			res.set(i,temp);
		}
		return res;
	}
	public List<equip> get_equip_by_id(String equip_id){
		return get_all_equip("select * from equip where equip_id=\""+equip_id+"\"");
	}
	
	public void edit_hero(hero h){
		int id=h.getHero_id();
		String name=h.getHero_name();
		String attribute=h.getImg_url()+'$'+h.getHero_attribute();
		int cost=h.getHero_cost();
		String skill=h.getHero_skill();
		String sql=String.format("update hero set hero_name=\"%s\", hero_attribute=\"%s\", hero_cost=\"%d\", hero_skill=\"%s\" where hero_id=\"%d\" ", name,attribute,cost,skill,id);
		d_hero.change(sql);
	}
	
	public void edit_trammel(trammel t){
		int id=t.getTrammel_id();
		String name=t.getTrammel_name();
		String description=t.getImg_url()+'$'+t.getTrammel_description();
		String number=t.getTrammel_number();
		String sql=String.format("update trammel set trammel_name=\"%s\", trammel_description=\"%s\", trammel_number=\"%s\" where trammel_id=\"%d\" ", name,description,number,id);
		d_trammel.change(sql);
	}
	public void edit_hex(hex h){
		int id=h.getHex_id();
		String name=h.getHex_name();
		String description=h.get_type()+'^'+h.getImg_url()+'$'+h.getHex_description();
		String sql=String.format("update hex set hex_name=\"%s\",hex_description=\"%s\" where hex_id=\"%d\" ", name,description,id);
	
		d_hex.change(sql);
	}
	public void edit_equip(equip e){
		int id=e.getEquip_id();
		String name=e.getEquip_name();
		String description=e.getType()+'^'+e.getImg_url()+'$'+e.getEquip_description();
		String sql=String.format("update equip set equip_name=\"%s\", equip_description=\"%s\" where equip_id=\"%d\"", name,description,id);
		d_equip.change(sql);
	}
	
}
